package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.*;
import com.coolwen.service.AddressService;
import com.coolwen.service.OrdersService;
import com.coolwen.service.ProductService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by user on 2016/6/22.
 */
@Controller
@RequestMapping("/user/order/")
public class OrdersController {
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private ProductService productService;

    @Autowired
    private AddressService addService;

    @Autowired
    private OrdersService ordersService;

    @RequestMapping("{id}/add")
    public String addToCart(@PathVariable int id, HttpSession session, Model model) {
        try {
            ShopCart shopCart = (ShopCart) session.getAttribute("shopCart");
            if (shopCart == null) {
                shopCart = new ShopCart();
                session.setAttribute("shopCart", shopCart);
            }
            Product p = productService.load(id);
            logger.info("OrdersController-------p------>>>>>>>>>" + JSON.toJSON(p));
            shopCart.add(p);
            logger.info("OrdersController-------shopCart------>>>>>>>>>" + JSON.toJSON(shopCart));
        } catch (ShopException e) {
            e.printStackTrace();
        }

        return "redirect:../show";
    }

    @RequestMapping("show")
    public String showCart(HttpSession session, Model model) {
        User lu = (User) session.getAttribute("loginUser");

        if (lu != null) {
            logger.info("OrdersController-------loginUser------->>>>>>>>>" + JSON.toJSON(lu));

            model.addAttribute("addresses", addService.listByUid(lu.getId()));
            logger.info("OrdersController-------addresses------->>>>>>>>>" + JSON.toJSON(addService.listByUid(lu.getId())));
        }
        return "order/showcart";
    }

    @RequestMapping("clearShopCart")
    public String clearShopCart(HttpSession session, Model model) {
        ShopCart shopCart = (ShopCart) session.getAttribute("shopCart");
        if (shopCart != null) {
            shopCart.clearShopCart();
        }

        return "redirect:show";
    }

    @RequestMapping("{id}/clearProduct")
    public String clearProduct(@PathVariable int id, HttpSession session, Model model) {
        ShopCart shopCart = (ShopCart) session.getAttribute("shopCart");
        if (shopCart != null) {
            shopCart.clearProduct(id);
        }

        return "redirect:../show";
    }

    @RequestMapping("{id}/addnumber")
    public String productAddNumberInput(@PathVariable int id) {
        productService.load(id);
        return "order/productAddNumberInput";
    }

    @RequestMapping("addOrders")
    public String addOrders(HttpSession session, HttpServletRequest req) {
        int aid = Integer.parseInt(req.getParameter("address"));
        double price = Double.parseDouble(req.getParameter("totalPrice"));
        Orders o = new Orders();
        o.setBuyDate(new Date());
        o.setStatus(1);
        o.setPrice(price);
        User u = (User) session.getAttribute("loginUser");
        ordersService.add(o, u, aid,
                ((ShopCart) session.getAttribute("shopCart")).getProducts());
        return "order/showcart";
    }

}
